//
//  ContributionListView.swift
//  SmartHousekeeper
//
//  Created by 张磊 on 2020/2/21.
//  Copyright © 2020 LS. All rights reserved.
//

import UIKit

class ContributionListView: UIView  ,UITableViewDelegate, UITableViewDataSource {
    
    
    typealias ContributionListViewSelectBankBlock = ()->()
    public var selectBankBlock : ContributionListViewSelectBankBlock? = nil
    
    typealias ContributionListViewSwitchBlock = (_ isOn:Bool)->()
    public var switchBlock : ContributionListViewSwitchBlock? = nil
    
    typealias ContributionListViewDidSelectTableViewBlock = (_ IndexPath:NSIndexPath)->()
    public var didSelectTableViewBlock : ContributionListViewDidSelectTableViewBlock? = nil
    
    lazy var tablewView : UITableView = {
        let value = UITableView(frame: CGRect.zero, style:.grouped)
        value.delegate = self
        value.dataSource = self
        value.separatorStyle = .none
        value.backgroundColor = .clear
        value.bounces = false
        return value
    }()
    let array =
        [["imgLogo":"icon_wxjf", "title":"物业费缴费"],
         ["imgLogo":"icon_cwjf", "title":"车位费用缴费"],
         ["imgLogo":"icon_fwjf", "title":"房屋租赁费"],
         ["imgLogo":"icon_sfjf", "title":"水费"],
         ["imgLogo":"icon_dfjf", "title":"电费"],
         ["imgLogo":"icon_rqjf", "title":"燃气费"],
         ["imgLogo":"icon_kdjf", "title":"宽带套餐费"],
         ["imgLogo":"icon_yxdsjf", "title":"有限电视费"],
         ["imgLogo":"icon_sjjf", "title":"手机话费"]]
    
    
    required init?(coder: NSCoder) {
        super .init(coder: coder)
    }
    
    override init(frame: CGRect) {
        super .init(frame: frame)
        setUI()
    }
    func setUI(){
        self.addSubview(tablewView)
        tablewView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return Adapter(50)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.didSelectTableViewBlock? (indexPath as NSIndexPath)
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "ContributionListTableViewCell") as? ContributionListTableViewCell
        if cell == nil{
            cell = ContributionListTableViewCell.init(style: .default, reuseIdentifier: "ContributionListTableViewCell")
        }
        cell?.selectionStyle = .none
        cell?.backgroundColor = .clear
        cell?.titleL.text = array[indexPath.row]["title"]
        cell?.imgLogo.image = UIImage(named: array[indexPath.row]["imgLogo"] ?? "")
        return cell!
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 200
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = ContributionListTableViewHeader(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 200))
        header.selectBankBtn.sce.add(.touchUpInside) { (btn) in
            if self.selectBankBlock != nil {
                self.selectBankBlock!()
            }
        }
        header.switchV.sce.add(.valueChanged) { (switchV) in
            
            self.switchBlock?(switchV.isOn)
        }
        
        return header
    }
    
}
